<!DOCTYPE html>
<html lang="en">
	<head>
		<title>threejs webgl - inline tone mapping</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				color: #fff;
				font-family:Monospace;
				font-size:13px;
				text-align:center;

				background-color: #000;

				margin: 0px;
				overflow: hidden;
			}

			a { color: #ff2 }

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
			}
		</style>
	</head>
	<body>

		<div id="container"></div>
		<div id="info"><a href="http://threejs.org" target="_blank">threejs</a> - Inline Tone Mapping (within a Material's fragment shader) without<br/>using a pre-processing step or float/half buffers by <a href="http://clara.io/" target="_blank">Ben Houston</a>.</div>

		<script src="../build/three.js"></script>
		<script src="js/controls/OrbitControls.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script src="js/libs/dat.gui.min.js"></script>
		<script src="js/loaders/RGBELoader.js"></script>
		<script src="js/loaders/HDRCubeTextureLoader.js"></script>
		<script src="js/pmrem/PMREMGenerator.js"></script>
		<script src="js/pmrem/PMREMCubeUVPacker.js"></script>

		<script src="js/postprocessing/EffectComposer.js"></script>
		<script src="js/postprocessing/RenderPass.js"></script>
		<script src="js/postprocessing/MaskPass.js"></script>
		<script src="js/postprocessing/ShaderPass.js"></script>
		<script src="js/shaders/CopyShader.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;
			var params = {
				opacity: 1.0,
				roughness: 1.0,
				bumpScale: 1.0,
				exposure: 3.0,
				whitePoint: 5.0,
				toneMapping: "Uncharted2",
				renderMode: "Renderer"
			};

			var toneMappingOptions = {
				None: THREE.NoToneMapping,
				Linear: THREE.LinearToneMapping,
				Reinhard: THREE.ReinhardToneMapping,
				Uncharted2: THREE.Uncharted2ToneMapping,
				Cineon: THREE.CineonToneMapping
			};

			var camera, scene, renderer, controls, group;
			var composer;
			var standardMaterial, standardMaterialPremultiplied, floorMaterial;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.set( 0.0, 40, 40 * 3.5 );

				scene = new THREE.Scene();

				renderer = new THREE.WebGLRenderer( { antialias: false } );

				standardMaterial = new THREE.MeshStandardMaterial( {
					bumpScale: - 0.05,
					color: 0xffffff,
					metalness: 0.9,
					roughness: 0.8,
					shading: THREE.SmoothShading,
					premultipliedAlpha: true,
					transparent: true
				} );

				var textureLoader = new THREE.TextureLoader();
				textureLoader.load( "textures/brick_diffuse.jpg", function( map ) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 4;
					map.repeat.set( 9, 0.5 );
					standardMaterial.map = map;
					standardMaterial.needsUpdate = true;
				} );
				textureLoader.load( "textures/brick_bump.jpg", function( map ) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 4;
					map.repeat.set( 9, 0.5 );
					standardMaterial.bumpMap = map;
					standardMaterial.needsUpdate = true;
				} );
				textureLoader.load( "textures/brick_roughness.jpg", function( map ) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 4;
					map.repeat.set( 9, 0.5 );
					standardMaterial.roughnessMap = map;
					standardMaterial.needsUpdate = true;
				} );

				group = new THREE.Group();
				scene.add( group );

				var geometry = new THREE.TorusKnotGeometry( 18, 8, 150, 20 );
				var mesh = new THREE.Mesh( geometry, standardMaterial );
				mesh.castShadow = true;
				mesh.receiveShadow = true;
				group.add( mesh );

				floorMaterial = new THREE.MeshStandardMaterial( {
					map: null,
					roughnessMap: null,
					color: 0x888888,
					metalness: 0.0,
					roughness: 1.0,
					side: THREE.BackSide
				} );

				var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
				var mesh = new THREE.Mesh( geometry, floorMaterial );
				mesh.position.y = 50;
				mesh.rotation.x = - Math.PI * 0.5;
				mesh.receiveShadow = true;
				scene.add( mesh );

				// Materials
				var hdrpath = "textures/cube/pisaHDR/";
				var hdrformat = '.hdr';
				var hdrurls = [
					hdrpath + 'px' + hdrformat, hdrpath + 'nx' + hdrformat,
					hdrpath + 'py' + hdrformat, hdrpath + 'ny' + hdrformat,
					hdrpath + 'pz' + hdrformat, hdrpath + 'nz' + hdrformat
				];

				var hdrCubeMap = new THREE.HDRCubeTextureLoader().load( THREE.UnsignedByteType, hdrurls, function ( hdrCubeMap ) {

					var pmremGenerator = new THREE.PMREMGenerator( hdrCubeMap );
					pmremGenerator.update( renderer );

					var pmremCubeUVPacker = new THREE.PMREMCubeUVPacker( pmremGenerator.cubeLods );
					pmremCubeUVPacker.update( renderer );

					standardMaterial.envMap = pmremCubeUVPacker.CubeUVRenderTarget.texture;
					standardMaterial.needsUpdate = true;

				} );

				// Lights

				scene.add( new THREE.HemisphereLight( 0x111111, 0x000000 ) );

				var spotLight = new THREE.SpotLight( 0xffffff );
				spotLight.position.set( 50, 100, 50 );
				spotLight.angle = Math.PI / 7;
				spotLight.decay = 2;
				spotLight.distance = 300;
				spotLight.penumbra = 0.8;
				spotLight.castShadow = true;
				scene.add( spotLight );

				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.shadowMap.enabled = true;
				container.appendChild( renderer.domElement );

				renderer.gammaInput = true;
				renderer.gammaOutput = true;

				composer = new THREE.EffectComposer( renderer );
				composer.setSize( window.innerWidth, window.innerHeight );

				var renderScene = new THREE.RenderPass( scene, camera );
				composer.addPass( renderScene );

				var copyPass = new THREE.ShaderPass( THREE.CopyShader );
				copyPass.renderToScreen = true;
				composer.addPass( copyPass );

				stats = new Stats();
				container.appendChild( stats.dom );

				controls = new THREE.OrbitControls( camera, renderer.domElement );
				controls.target.set( 0, 0, 0 );
				controls.update();

				window.addEventListener( 'resize', onWindowResize, false );

				var gui = new dat.GUI();

				gui.add( params, 'toneMapping', Object.keys( toneMappingOptions ) );
				gui.add( params, 'exposure', 0, 10 );
				gui.add( params, 'whitePoint', 0, 10 );
				gui.add( params, 'opacity', 0, 1 );
				gui.add( params, 'renderMode', [ 'Renderer', 'Composer'] );
				gui.open();

			}

			function onWindowResize() {

				var width = window.innerWidth;
				var height = window.innerHeight;

				camera.aspect = width / height;
				camera.updateProjectionMatrix();

				renderer.setSize( width, height );
				composer.setSize( width, height );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				stats.begin();
				render();
				stats.end();

			}

			function render() {

				if ( standardMaterial !== undefined ) {


					standardMaterial.roughness = params.roughness;
					standardMaterial.bumpScale = - 0.05 * params.bumpScale;
					standardMaterial.opacity = params.opacity;

				}
				if( renderer.toneMapping !== toneMappingOptions[ params.toneMapping ] ) {
					renderer.toneMapping = toneMappingOptions[ params.toneMapping ];
					if( standardMaterial ) standardMaterial.needsUpdate = true;
					if( floorMaterial ) floorMaterial.needsUpdate = true;
				}
				renderer.toneMappingExposure = params.exposure;
				renderer.toneMappingWhitePoint = params.whitePoint;

				var timer = Date.now() * 0.00025;

				camera.lookAt( scene.position );

				for ( var i = 0, l = group.children.length; i < l; i ++ ) {

					var object = group.children[ i ];
					object.rotation.y += 0.005;

				}

				if( params.renderMode === "Composer" ) {
					composer.render();
				}
				else {
					renderer.render( scene, camera );
				}

			}

		</script>

	</body>
</html>
